package cn.com.demo.cert;

import cn.com.demo.util.Utils;

import java.io.ByteArrayInputStream;
import java.security.cert.CertPath;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Arrays;

public class CertPathExample {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        X509Certificate[] certs = X509CertificateExample.buildCertificateChain();
        CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
        CertPath certPath = cf.generateCertPath(Arrays.asList(certs));
        byte[] encoded = certPath.getEncoded("PEM");

        System.out.println(Utils.toString(encoded));

        CertPath newCertPath = cf.generateCertPath(new ByteArrayInputStream(encoded), "PEM");

        if (newCertPath.equals(certPath)) {
            System.out.println("CertPath recovered correctly.");
        }
    }

}
